<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <el-pagination
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :pager-count="pagerCount"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { scrollTo } from "@/utils/scroll-to";

export default {
  name: "Pagination",
  props: {
    total: {
      required: true,
      type: Number,
    },
    page: {
      type: Number,
      default: 1,
    },
    limit: {
      type: Number,
      default: 20,
    },
    pageSizes: {
      type: Array,
      default() {
        return [10, 20, 50];
      },
    },
    // 移动端页码按钮的数量端默认值5
    pagerCount: {
      type: Number,
      default: document.body.clientWidth < 992 ? 5 : 7,
    },
    // default: 'total, prev, pager, next, jumper,sizes'
    layout: {
      type: String,
      default: "total,sizes,prev, pager, next, jumper",
    },
    background: {
      type: Boolean,
      default: true,
    },
    autoScroll: {
      type: Boolean,
      default: true,
    },
    hidden: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    currentPage: {
      get() {
        return this.page;
      },
      set(val) {
        this.$emit("update:page", val);
      },
    },
    pageSize: {
      get() {
        return this.limit;
      },
      set(val) {
        this.$emit("update:limit", val);
      },
    },
  },
  methods: {
    handleSizeChange(val) {
      this.$emit("pagination", { page: this.currentPage, limit: val });
      if (this.autoScroll) {
        scrollTo(0, 800);
      }
    },
    handleCurrentChange(val) {
      this.$emit("pagination", { page: val, limit: this.pageSize });
      if (this.autoScroll) {
        scrollTo(0, 800);
      }
    },
  },
};
</script>

<style lang="scss" scope>
.pagination-container {
  background: #fff;
  padding: 10px 0;
}

.pagination-container.hidden {
  display: none;
}
.el-select-dropdown__item.selected,
.el-select-dropdown__item.hover,
.el-select-dropdown__item:hover {
  margin-left: 6px;
  margin-right: 6px;
  color: #1d6fe9;
  font-size: 14px;
  font-weight: 400;
  border-radius: 4px;
}
.el-select-dropdown__item {
  margin-left: 6px;
  margin-right: 6px;
}
</style>
<style lang="scss">
.pagination-container {
  font-family: "PingFangSC-Regular, PingFang SC";
  font-weight: 400;
  text-align: right;
  position: fixed;
  bottom: 0;
  right: 12px;
  box-sizing: border-box;
  width: 100%;
  background-color: #fff;
  z-index: 99;
  .el-input--mini .el-input__inner {
    height: 32px;
    line-height: 32px;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 3px;
    border: 1px solid #dcdcdc;
    font-size: 14px;
    color: rgba(0, 0, 0, 0.9);
  }
  .el-pagination {
    padding: 0;
    .btn-prev {
      background: rgba(255, 255, 255, 0.9);
      border: 0;
      color: rgba(0, 0, 0, 0.6);
    }
    .btn-next {
      background-color: transparent;
      color: rgba(0, 0, 0, 0.6);
    }
    .el-pagination__sizes {
      height: 32px;
      line-height: 32px;
      margin-right: 0;
      .el-input {
        margin: 0;
      }
    }
    .el-pager {
      .number {
        width: 32px;
        height: 32px;
        background: rgba(255, 255, 255, 0.9);
        border-radius: 3px;
        border: 1px solid #dcdcdc;
        line-height: 30px;
        padding: 0;
        font-size: 14px;
        color: rgba(0, 0, 0, 0.6);
        font-weight: 400;
        box-sizing: border-box;
        margin: 0 4px;
      }

      .active {
        background-color: #1d6fe9 !important;
        color: rgba(255, 255, 255, 0.9) !important;
        border: 0;
      }

      .more {
        background-color: transparent;
        color: rgba(0, 0, 0, 0.6);
      }
    }
  }
  .el-pagination.is-background .el-pager li:not(.disabled):hover {
    color: #1d6fe9;
  }
}
</style>
